package uk.ac.glasgow.demosocs.impl.ui;

import java.util.Date;

import uk.ac.glasgow.clui.ArgumentsDescription;
import uk.ac.glasgow.clui.CommandDescription;
import uk.ac.glasgow.clui.MinimumArguments;
import uk.ac.glasgow.clui.SystemCommand;
import uk.ac.glasgow.clui.SystemCommandException;
import uk.ac.glasgow.demosocs.Election;
import uk.ac.glasgow.demosocs.VotingSystem;
import uk.ac.glasgow.demosocs.impl.Referendum;
import uk.ac.glasgow.demosocs.impl.AlternativeVote;

@MinimumArguments(9)
@CommandDescription("Create a new election with the specified parameters.")
@ArgumentsDescription("<name> <type> <nomination day> <nomination time> <start day> <start time> <stop day> <stop time> <electorate ID>")
public class CreateElectionCommand extends SystemCommand<VotingSystem> {

	public CreateElectionCommand(VotingSystem facade) {
		super(facade);
	}

	@Override
	public String processCommand(String[] arguments)
			throws SystemCommandException {
		String name = arguments[0];
		String type = arguments[1];
		boolean available = false;
		
		String[] electionTypes = {"FPTP", "REF", "ALTV"};
		for(String str: electionTypes)
			if(type.equalsIgnoreCase(str))
				available = true;
				
		if(!available){
			return "Invalid Election type. Only FPTP, REF or ALTV are supported.";
		}
		
		
		/* WHO REPLACED TWO LINES OF COE WITH SEVEN LOL ^^^^^
		if (!type.equalsIgnoreCase("FPTP") ||  !type.equalsIgnoreCase("REF") || !type.equalsIgnoreCase("ALTV")){
			return "Invalid Election type. Only FPTP, REF or ALTV are supported.";
		}
		*/
		facade.createElection(name, type);
	
		if (facade.getCurrentElection() == null)
			return "Failed to create election. Are you a manager?";
	
		if (type.equalsIgnoreCase("FPTP")){
			Date openNominations = DateUtil.parseArgumentDate(arguments[2],arguments[3]);
			Date start = DateUtil.parseArgumentDate(arguments[4],arguments[5]);
			Date stop = DateUtil.parseArgumentDate(arguments[6],arguments[7]);
			
			String electorateID = arguments[8];
			
			facade.editElection(openNominations, start, stop, electorateID);
			//facade.createElection(name, type, openNominations, start, stop, electorateID);

			return "Created election with name ["+name+"], type ["+type+"] and electorate ["+electorateID+"].";
		}
		else if (type.equalsIgnoreCase("REF")){
			Date start = DateUtil.parseArgumentDate(arguments[2],arguments[3]);
			Date stop = DateUtil.parseArgumentDate(arguments[4],arguments[5]);
			String question = arguments[6];
			String EID = arguments[7];
			//used so the create ref command passes the min arguments required
			String uselessArgument = arguments[8];			
			uselessArgument = "test";
			
			((Referendum)facade.getCurrentElection()).setQuestion(question);
			((Referendum)facade.getCurrentElection()).setStart(start);
			((Referendum)facade.getCurrentElection()).setEnd(stop);
			((Referendum)facade.getCurrentElection()).setEID(EID);
			//((Referendum)facade.getCurrentElection()).set
			
			return "Create referendum with name ["+name+"], question "+question+" type ["+type+"] and electorate"+EID;
		}
		else {
			Date openNominations = DateUtil.parseArgumentDate(arguments[2],arguments[3]);
			Date start = DateUtil.parseArgumentDate(arguments[4],arguments[5]);
			Date stop = DateUtil.parseArgumentDate(arguments[6],arguments[7]);
			
			String electorateID = arguments[8];
			
			
			facade.editElection(openNominations, start, stop, electorateID);
			//extra line of code to edit the Alternative Vote specific variables
			int choices = Integer.parseInt(arguments[9]);
			System.out.println("Choices =" + choices);
			AlternativeVote AVElection = ((AlternativeVote)facade.getCurrentElection());
			Election election = facade.getCurrentElection();
			
			if(election == null)
				System.out.println("Election is null");
			else if (AVElection == null)
				System.out.println("AVElection is null");
			int test = AVElection.getNumberOfChoices() + 1;
			System.out.println("Test =" + test);
			return "Created Alternative Vote election with name ["+name+"], type ["+type+"] and electorate ["+electorateID+"].";
		}
	}
}
